package com.sanuo.pico.act.work;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.menu.app.util.Utils;
import com.sanuo.pico.R;
import com.sanuo.pico.databinding.ActivityMsnBinding;
import com.sanuo.pico.it.SettingPage;
import com.sanuo.pico.utils.Command;
import com.sanuo.pico.utils.Const;

public class MsnActivity extends BaseWorkActivity implements SettingPage {
    ActivityMsnBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMsnBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.back.setOnClickListener(v -> {
            Command.sendTouchEvent(0, 0x20);
            if (Const.jump) {
                finish();
            }
        });

        if (Const.pwd != null && Const.pwd.startsWith("158")) {

        } else {
            binding.tv2.setVisibility(TextView.INVISIBLE);
            binding.bottom.setVisibility(TextView.INVISIBLE);
        }
        binding.title.setText(getResources().getStringArray(R.array.settings)[4]);
        binding.tv.setText(Utils.getMsn());

        binding.save.setOnClickListener(v -> {
            save();
        });
        binding.tv2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH
                        || (event != null && actionId == EditorInfo.IME_ACTION_UNSPECIFIED && event.getAction() == KeyEvent.ACTION_DOWN)
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null
                        && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event
                        .getAction() == KeyEvent.ACTION_DOWN)) {
                    save();
                    return true;
                }
                return false;
            }
        });
    }

    private void save() {
        Utils.hideInput(binding.tv2);
        String str = binding.tv2.getText().toString().trim();
        if (TextUtils.isEmpty(str)) {
            return;
        }
        Utils.setMsn(str);
        binding.tv.setText(str);
    }
}
